package com.kayang.feishu.controller;


import com.kayang.common.HttpMsg;
import com.kayang.common.util.MapUtil;
import com.kayang.feishu.config.FeiShuConfig;
import com.kayang.feishu.pojo.param.DelUserParam;
import com.kayang.feishu.service.AccessTokenService;
import com.kayang.feishu.pojo.FeiShuContext;
import com.kayang.feishu.pojo.UserModel;
import com.kayang.feishu.service.UserService;
import com.kayang.nadapt.util.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController{
    @Autowired
    AccessTokenService tokenService;

    @Autowired
    FeiShuConfig feiShuConfig;

    @Autowired
    UserService userService;

    @PostMapping("/create")
    public ResponseEntity creatUser(@Valid  @RequestBody UserModel userModel){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/users?client_token={client_token}&department_id_type={department_id_type}&user_id_type={user_id_type}";
        Map param = MapUtil.toMap(userModel);
        url = url.replace("{client_token}", Convert.toString(param.get("client_token")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
        param.remove("client_token");
        param.remove("department_id_type");
        param.remove("user_id_type");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(param);
        Map headers = new HashMap();
        headers.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        headers.put("Content-Type", "application/json; charset=utf-8");
        Map ret = userService.creatUser(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PostMapping("/update")
    public ResponseEntity updateUser(@RequestBody Map param){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/users/{user_id}?department_id_type={department_id_type}&user_id_type={user_id_type}";
        url = url.replace("{user_id}", Convert.toString(param.get("user_id")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
        param.remove("client_token");
        param.remove("department_id_type");
        param.remove("user_id_type");
        param.remove("user_id");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("PATCH");
        context.setBody(param);
        Map headers = new HashMap();
        headers.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        headers.put("Content-Type", "application/json; charset=utf-8");
        Map ret = userService.updateUser(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("修改成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    /*
    *param:
    * {
        "userid":"user123"
       }
    *
    * */
    @PostMapping ("/delete")
    public ResponseEntity deleteUser(@Valid @RequestBody DelUserParam delUserParam){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/users/{user_id}?user_id_type={user_id_type}";
        Map param = MapUtil.toMap(delUserParam);
        url = url.replace("{user_id}", Convert.toString(param.get("user_id")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")));
        param.remove("user_id_type");
        param.remove("user_id");
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("DELETE");
        context.setBody(param);
        Map headers = new HashMap();
        headers.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        headers.put("Content-Type", "application/json; charset=utf-8");
        Map ret = userService.updateUser(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    @PostMapping ("/get")
    public ResponseEntity getUser(@RequestBody Map param){
        String url = feiShuConfig.getPath() + "/open-apis/contact/v3/users/{user_id}?user_id_type={user_id_type}&department_id_type={department_id_type}";
        url = url.replace("{user_id}", Convert.toString(param.get("user_id")))
                .replace("{user_id_type}", Convert.toString(param.get("user_id_type")))
                .replace("{department_id_type}", Convert.toString(param.get("department_id_type")));
        FeiShuContext context = new FeiShuContext();
        context.setUrl(url);
        context.setMethod("GET");
        context.setBody(param);
        Map header = new HashMap();
        header.put("Authorization", "Bearer " + tokenService.getAccessTokenStr(feiShuConfig.getAppKey()));
        header.put("Content-Type", "application/json; charset=utf-8");
        Map ret = userService.getUser(context, header);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查看成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }



}
